package com.gusti.admincasa.controller.impl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.gusti.admincasa.controller.IAdminCasaController;
import com.gusti.admincasa.services.IAdminCasaService;
import com.gusti.admincasa.services.IParallelPortService;
import com.gusti.admincasa.util.ConstantsUtil;
import com.gusti.admincasa.util.AdminCasaResponse;

/**
 * 
 * @author gusti
 *
 */
@Controller
public class AdminCasaController implements IAdminCasaController {

	@Autowired
	IAdminCasaService adminCasaService;
	@Autowired
	IParallelPortService parallelPortService;

	@RequestMapping(value = "/iamhere/{data}", method = RequestMethod.GET)
	@ResponseBody
	public AdminCasaResponse iamHere( @PathVariable("data") String jsonData ) {

		return adminCasaService.iamHere( jsonData );
	}

	@RequestMapping(value = "/users", method = RequestMethod.GET)
	@ResponseBody
	public AdminCasaResponse listUsers() {

		return adminCasaService.listUsers();
	}

	@RequestMapping(value = "/{userName}", method = RequestMethod.GET)
	@ResponseBody
	public AdminCasaResponse viewUser( @PathVariable("userName") String userName ) {

		return adminCasaService.viewUser( userName );
	}

	@RequestMapping(value = "/message/{userName}/{messageText}", method = RequestMethod.GET)
	@ResponseBody
	public AdminCasaResponse newMessage( @PathVariable("userName") String userName, @PathVariable("messageText") String messageText ) {

		return adminCasaService.newMessage( userName, messageText );
	}

	@RequestMapping(value = "/message/{userName}/{messageText}/{senderName}", method = RequestMethod.GET)
	@ResponseBody
	public AdminCasaResponse newMessage( @PathVariable("userName") String userName, @PathVariable("messageText") String messageText, @PathVariable("senderName") String senderName ) {

		return adminCasaService.newMessage( userName, messageText, senderName );
	}

	@RequestMapping(value = "/message/read/{ownerId}/{messageId}", method = RequestMethod.GET)
	@ResponseBody
	public AdminCasaResponse markAsReadMessage( @PathVariable("ownerId") long ownerId, @PathVariable("messageId") long messageId ) {

		return adminCasaService.markAsReadMessage( ownerId, messageId );
	}

	@RequestMapping(value = "/pp", method = RequestMethod.GET)
	@ResponseBody
	public AdminCasaResponse viewPortStatus() {

		return parallelPortService.viewPortStatus();
	}

	@RequestMapping(value = "/pp/reset", method = RequestMethod.GET)
	@ResponseBody
	public AdminCasaResponse resetPortStatus() {

		return parallelPortService.resetPortStatus();
	}

	@RequestMapping(value = "/pp/{portNumber}/{activated}", method = RequestMethod.GET)
	@ResponseBody
	public AdminCasaResponse updatePortStatus( @PathVariable("portNumber") int portNumber, @PathVariable("activated") boolean activated ) {

		return parallelPortService.updatePortStatus( portNumber, activated );
	}

	@RequestMapping(value = "/404", method = RequestMethod.GET)
	@ResponseBody
	public AdminCasaResponse error404() {
		AdminCasaResponse messageResponse = new AdminCasaResponse();
		messageResponse.setErrorMsg( ConstantsUtil.ERROR_404 );
		return messageResponse;
	}

	@RequestMapping(value = "/500", method = RequestMethod.GET)
	@ResponseBody
	public AdminCasaResponse error500() {
		AdminCasaResponse messageResponse = new AdminCasaResponse();
		messageResponse.setErrorMsg( ConstantsUtil.ERROR_500 );
		return messageResponse;
	}

}
